package com.snap.vseries.dialog;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.InputFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.snap.vseries.R;
import com.snap.vseries.utils.ToastUtils;

import java.math.BigDecimal;

public class NumberInputDialog extends DialogFragment {

    private OnValueListener listener;
    private int dotCount;
    private float value;
    private float maxValue;
    private float minValue;
    private String attrName;
    private String unitName;
    private int maxLength = 6;
    private boolean showNegative = false;

    private TextView attrNameTv;

    private TextView valueEt;

    private TextView num0;
    private TextView num1;
    private TextView num2;
    private TextView num3;
    private TextView num4;
    private TextView num5;
    private TextView num6;
    private TextView num7;
    private TextView num8;
    private TextView num9;

    private TextView dot;
    private View dotLine;

    private TextView negative;
    private View negativeLine;

    private ImageView back;
    private ImageView clear;
    private ImageView confirm;

    public NumberInputDialog setParam(int dotCount, float minValue, float value, float maxValue,
                                      String attrName, String unitName, OnValueListener listener) {
        this.dotCount = dotCount;
        this.maxValue = maxValue;
        this.minValue = minValue;
        this.value = value;
        this.attrName = attrName;
        this.unitName = unitName;
        this.listener = listener;
        return this;
    }

    public NumberInputDialog setMaxLength(int length) {
        this.maxLength = length;
        return this;
    }

    public NumberInputDialog setNegativeOption() {
        this.showNegative = true;
        return this;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, R.style.DimDialogStyle);
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().setCanceledOnTouchOutside(true);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.dialog_number_input, container, false);

        attrNameTv = v.findViewById(R.id.attr_tv);
        valueEt = v.findViewById(R.id.value_et);

        num0 = v.findViewById(R.id.num0);
        num1 = v.findViewById(R.id.num1);
        num2 = v.findViewById(R.id.num2);
        num3 = v.findViewById(R.id.num3);
        num4 = v.findViewById(R.id.num4);
        num5 = v.findViewById(R.id.num5);
        num6 = v.findViewById(R.id.num6);
        num7 = v.findViewById(R.id.num7);
        num8 = v.findViewById(R.id.num8);
        num9 = v.findViewById(R.id.num9);

        dot = v.findViewById(R.id.dot);
        dotLine = v.findViewById(R.id.dot_cutline);
        negative = v.findViewById(R.id.negative);
        negativeLine = v.findViewById(R.id.negative_cutline);
        back = v.findViewById(R.id.back);
        clear = v.findViewById(R.id.clear);
        confirm = v.findViewById(R.id.confirm);

        dot.setVisibility(dotCount > 0 ? View.VISIBLE : View.GONE);
        dotLine.setVisibility(dotCount > 0 ? View.VISIBLE : View.GONE);
        negative.setVisibility(showNegative ? View.VISIBLE : View.GONE);
        negativeLine.setVisibility(showNegative ? View.VISIBLE : View.GONE);

        attrNameTv.setText(attrName + ((unitName == null || unitName.isEmpty()) ? "" : "(" + unitName + ")"));
        valueEt.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
        valueEt.setText(new BigDecimal(String.valueOf(value)).stripTrailingZeros().toPlainString());

        View.OnClickListener numListener = v12 -> {
            String cur = valueEt.getText().toString().trim();
            if (cur.contains(".")) {
                String[] sp = cur.split("\\.");
                if (sp.length > 1 && sp[1].length() >= dotCount) {
                    return;
                }
            }
            if (cur.equals("0")) {
                cur = "";
            }
            switch (v12.getId()) {
                case R.id.num0:
                    cur += "0";
                    break;
                case R.id.num1:
                    cur += "1";
                    break;
                case R.id.num2:
                    cur += "2";
                    break;
                case R.id.num3:
                    cur += "3";
                    break;
                case R.id.num4:
                    cur += "4";
                    break;
                case R.id.num5:
                    cur += "5";
                    break;
                case R.id.num6:
                    cur += "6";
                    break;
                case R.id.num7:
                    cur += "7";
                    break;
                case R.id.num8:
                    cur += "8";
                    break;
                case R.id.num9:
                    cur += "9";
                    break;
                default:
            }
            valueEt.setText(cur);
        };

        num0.setOnClickListener(numListener);
        num1.setOnClickListener(numListener);
        num2.setOnClickListener(numListener);
        num3.setOnClickListener(numListener);
        num4.setOnClickListener(numListener);
        num5.setOnClickListener(numListener);
        num6.setOnClickListener(numListener);
        num7.setOnClickListener(numListener);
        num8.setOnClickListener(numListener);
        num9.setOnClickListener(numListener);

        dot.setOnClickListener(v13 -> {
            if (dotCount == 0) {
                return;
            }
            String cur = valueEt.getText().toString().trim();
            if (cur.contains(".")) {
                return;
            }
            cur += ".";
            valueEt.setText(cur);
        });
        negative.setOnClickListener(v13 -> {
            String cur = valueEt.getText().toString().trim();
            if (cur.equals("0")) {
                return;
            } else if (cur.contains("-")) {
                cur = cur.substring(1);
                valueEt.setText(cur);
            } else {
                valueEt.setText("-" + cur);
            }
        });

        back.setOnClickListener(v13 -> {
            String cur = valueEt.getText().toString().trim();
            cur = cur.substring(0, cur.length() - 1);
            if (cur.length() == 0 || cur.equals("-")) {
                cur = "0";
            }
            valueEt.setText(cur);
        });
        clear.setOnClickListener(v13 -> valueEt.setText("0"));
        confirm.setOnClickListener(v13 -> {
            float resValue = Float.parseFloat(valueEt.getText().toString());
            if (resValue > maxValue || resValue < minValue) {
                ToastUtils.get().showText(String.format("输入超出范围[ %s , %s ]，请修改！",
                        new BigDecimal(String.valueOf(minValue)).stripTrailingZeros().toPlainString(),
                        new BigDecimal(String.valueOf(maxValue)).stripTrailingZeros().toPlainString()));
                return;
            }
            listener.onValueChanged(resValue);
            NumberInputDialog.this.dismiss();
        });
        return v;
    }

    public interface OnValueListener {
        void onValueChanged(float value);
    }
}